package com.realife.mbplus.controller;

import com.alibaba.fastjson.JSON;
import com.alpha.centity.gulisuper.Brand;
import com.realife.mbplus.service.BrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @version V1.0
 * @author: Albert
 * @date: 2020/5/26 21:49
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    @Qualifier("brandService11")
    private BrandService brandService;


    @RequestMapping("/show1")
    public ResponseEntity<List<Brand>> show1() {

        return ResponseEntity.ok(this.brandService.queryAllBrand());
    }


    @RequestMapping("/show2/{id}")
    public ResponseEntity<String> show2(@PathVariable("id") Long id) {

        return ResponseEntity.ok(this.brandService.updateById(id));
    }


    @RequestMapping("/show3")
    public ResponseEntity<String> show3() {

        return ResponseEntity.ok(this.brandService.insertBrand());
    }


    private static Lock lock = new ReentrantLock();

    @RequestMapping("/batch/insert/{num}")
    public ResponseEntity<String> show5(@PathVariable("num") Integer num) {

        lock.lock();

        List<String> resultMsg = new ArrayList<>();

        for (Integer i = 0; i < num; i++) {

            String result = this.brandService.insertBrand();
            resultMsg.add(result);

            try {
                TimeUnit.MILLISECONDS.sleep(0);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

        lock.unlock();

        return ResponseEntity.ok().body(JSON.toJSONString(resultMsg));
    }

}

